package com.bgs.service.impl;

import com.alibaba.fastjson.JSON;
import com.bgs.ThreeConfig.DirectExchangeTestThree;
import com.bgs.common.R;
import com.bgs.feign.ZeroFeign;
import com.bgs.pojo.OrderDetail;
import com.bgs.mapper.OrderDetailMapper;
import com.bgs.pojo.Picture;
import com.bgs.pojo.Store;
import com.bgs.pojo.vo.FourVideo;
import com.bgs.pojo.vo.OrderDetailTow;
import com.bgs.pojo.vo.FourOrderDetail;
import com.bgs.pojo.vo.ThreeDetPic;
import com.bgs.service.OrderDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 商品详情表 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper, OrderDetail> implements OrderDetailService {
    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private ZeroFeign zeroFeign;

    @Override
    public R zeroOrderDetailState4(Integer orderDetailId) {
        boolean b = orderDetailMapper.zeroOrderDetailState4(orderDetailId);
        return R.ok().data("zeroOrderDetailState4", b);
    }

    /**
     * 后台商品审核模块
     *
     * @return
     */
    @Override
    public List<OrderDetail> TowGettableDataShang(String orderDetailName, Integer storeId) {
        return orderDetailMapper.TowGettableDataShang(orderDetailName, storeId);
    }

    /**
     * 后台商品审核通过
     *
     * @param orderDetailId
     * @return
     */
    @Override
    public int TowShangShen(Integer orderDetailId) {
        return orderDetailMapper.TowShangShen(orderDetailId);
    }

    @Override
    public int TowShangShenNo(Integer orderDetailId) {
        return orderDetailMapper.TowShangShenNo(orderDetailId);
    }

    /**
     * 查询购物车商品
     *
     * @param storeId
     * @return
     */
    @Override
    public List<OrderDetail> TowOrderDetail(Integer storeId) {
        return orderDetailMapper.TowOrderDetail(storeId);
    }

    /**
     * 远程调用商品信息
     *
     * @param orderDetailId
     * @return
     */
    @Override
    public List<OrderDetailTow> TowgetOrderDetail(Integer orderDetailId) {
        return orderDetailMapper.TowgetOrderDetail(orderDetailId);
    }

    /**
     * 远程调用查看商品图片
     *
     * @param orderDetailId
     * @return
     */
    @Override
    public List<Picture> TowGetPictures(Integer orderDetailId) {
        return orderDetailMapper.TowGetPictures(orderDetailId);
    }


    @Override
    public List<OrderDetail> zeroOrderDetailIdToOrderDetail(Integer orderDetailId) {
        return orderDetailMapper.zeroOrderDetailIdToOrderDetail(orderDetailId);
    }
    //服务远程调用查看所有商品
    @Override
    public List<OrderDetail> TowOrderList(Integer storeId) {
        return orderDetailMapper.TowOrderList(storeId);
    }

    /**
     * 查询店铺下商品
     *
     * @param id
     * @return
     */
    @Override
    public R fourOpenShop(Integer id, Integer shangpinid) {
        Integer i = 0;
        if (id != null) {
            R r = zeroFeign.FourStoreId(id);
            Map<String, Object> map = new LinkedHashMap<>(r.getData());
            String id1 = map.get("id").toString();
            int i1 = id1.indexOf(',');
            String substring = id1.substring(9, i1);//改法获取第一个，号下表然后取出
            i = Integer.parseInt(substring);
        }
        System.out.println(121);
        List<OrderDetail> list = orderDetailMapper.fourOpenshop(i, shangpinid);
        return R.ok().data("data", list);
    }


    @Override
    public R fourAddWupin(FourOrderDetail fourOrderDetail) {

        System.out.println(fourOrderDetail);
        int i = orderDetailMapper.fourOrderDetail(fourOrderDetail);
        Integer id = fourOrderDetail.getOrderDetailId();
        int i1 = orderDetailMapper.fourAddImg(id,fourOrderDetail);
        if (fourOrderDetail.getVideoUrl()!=null && fourOrderDetail.getVideoUrl()!=""){
            int i2 = orderDetailMapper.fourVideo(id,fourOrderDetail);
        }
        return R.ok().data("data",i1);
    }
    /**
     * 查询物品详情
     *
     * @param id
     * @return
     */
    @Override
    public R fourQueryDetail(int id) {
        FourOrderDetail oo = orderDetailMapper.fourQueryDetail(id);
        return R.ok().data("data", oo);
    }

    @Override
    public R fourIndexShow() {
        List<Store> list1 = zeroFeign.FourStore();

        List<FourVideo> list = orderDetailMapper.fourIndexShow();
        for (FourVideo fourVideo : list) {
            for (Store store : list1) {
                if (fourVideo.getStoreId() == store.getStoreId()) {
                    fourVideo.setStoreName(store.getStoreName());
                    fourVideo.setStoreImg(store.getStoreImg());
                }
            }
        }
        return R.ok().data("data", list);
    }

    @Override
    public R ThreeSelectDetail(List<OrderDetail> list) {
        List<ThreeDetPic> ThreeDetail = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ThreeDetPic ThreeOrder = orderDetailMapper.ThreeSelectDetail(list.get(i).getOrderDetailId());
            ThreeDetail.add(ThreeOrder);
        }
        return R.ok().data("data", ThreeDetail);
    }

    @RabbitListener(queues = DirectExchangeTestThree.REPERTORY_DIRECT_EXCHANGE_QUEUE_NAME)
    public void DetailThree(Message msg) {
        String body = new String(msg.getBody());
        HashMap<String, String> res = JSON.parseObject(body, HashMap.class);
        Object detail = res.get("detail");
        int i1 = Integer.parseInt(detail.toString());
        Object value = res.get("value");
        Integer i2 = Integer.parseInt(value.toString());
        boolean i = orderDetailMapper.minRepertory(i1, i2);

    }
}
